package com.gitee.wsl.struct.grammar.ast

import com.gitee.wsl.base.Tuple2
import com.gitee.wsl.base.Tuple3
import com.gitee.wsl.base.Tuple4
import com.gitee.wsl.base.Tuple5
import com.gitee.wsl.base.Tuple6
import com.gitee.wsl.base.Tuple7
import com.gitee.wsl.base.toList
import com.gitee.wsl.struct.grammar.Grammar
import com.gitee.wsl.struct.grammar.ast.node.ASTNode
import com.gitee.wsl.struct.grammar.ast.node.CompositeNode
import com.gitee.wsl.struct.grammar.ast.node.LeafNode
import com.gitee.wsl.struct.grammar.ast.node.NodeType
import com.gitee.wsl.struct.grammar.parser.token.TokenMatch

typealias ASTNodeGrammar = Grammar<ASTNode>

fun ASTNodeGrammar.node(type: NodeType, literal: TokenMatch): ASTNode {
    return LeafNode(type, literal.offset, literal.nextOffset)
}

fun ASTNodeGrammar.node(type: NodeType, vararg children: ASTNode) = CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: List<ASTNode>) = CompositeNode(type, children)

fun ASTNodeGrammar.node(type: NodeType, children: Tuple2<ASTNode, ASTNode>) = CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: Tuple3<ASTNode, ASTNode, ASTNode>) =
    CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: Tuple4<ASTNode, ASTNode, ASTNode, ASTNode>) =
    CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: Tuple5<ASTNode, ASTNode, ASTNode, ASTNode, ASTNode>) =
    CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: Tuple6<ASTNode, ASTNode, ASTNode, ASTNode, ASTNode, ASTNode>) =
    CompositeNode(type, children.toList())

fun ASTNodeGrammar.node(type: NodeType, children: Tuple7<ASTNode, ASTNode, ASTNode, ASTNode, ASTNode, ASTNode, ASTNode>) =
    CompositeNode(type, children.toList())
